package com.example.gzysmartcity.ui.fragment.travel;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.SearchView;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.example.gzysmartcity.R;
import com.example.gzysmartcity.databinding.FragmentTravelBinding;
import com.example.gzysmartcity.repository.model.MyTravel;
import com.example.gzysmartcity.repository.model.SCTravel;
import com.example.gzysmartcity.repository.remote.State;
import com.example.gzysmartcity.ui.adapter.BrStringAdapter;
import com.example.gzysmartcity.ui.adapter.TravelRvAdapter;
import com.example.gzysmartcity.ui.base.BaseFragment;
import com.example.gzysmartcity.viewmodel.TravelViewModel;
import com.youth.banner.Banner;
import com.youth.banner.indicator.CircleIndicator;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class TravelFragment extends BaseFragment<FragmentTravelBinding> {
    @Override
    protected FragmentTravelBinding viewBinding() {
        return FragmentTravelBinding.inflate(getLayoutInflater(), baseBinding.getRoot(), true);
    }

    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        TravelViewModel travelViewModel = new ViewModelProvider(requireActivity()).get(TravelViewModel.class);
        b.rvTarvel.setLayoutManager(new LinearLayoutManager(getContext()));
        travelViewModel.getTravel();

        travelViewModel.getMutableLiveData().observe(getViewLifecycleOwner(), state -> {
            if (state == State.SUCCESS) {
                List<MyTravel> myTravelList = travelViewModel.getMyTravelList();
                List<SCTravel.DataBean> scTravelList = new ArrayList<>(travelViewModel.getScTravelList());
                //
                Banner<String, BrStringAdapter> banner = b.getRoot().findViewById(R.id.banner_travel);
                ArrayList<String> strings = new ArrayList<>();
                for (int i = 0; i < myTravelList.size(); i++) {
                    String s = myTravelList.get(i).getImgList().get(0);
                    strings.add(s);
                    scTravelList.get(i).setImgUrl(s);

                }
                banner.setAdapter(new BrStringAdapter(strings))
                        .addBannerLifecycleObserver(this)
                        .setLoopTime(2000)
                        .setIndicator(new CircleIndicator(getContext()));
                //

                TravelRvAdapter travelRvAdapter = new TravelRvAdapter(R.layout.rv_item_travel);
                travelRvAdapter.setNewInstance(scTravelList);
                b.rvTarvel.setAdapter(travelRvAdapter);
                travelRvAdapter.setOnItemClickListener((adapter, view1, position) -> {
                    Bundle bundle = new Bundle();
                    SCTravel.DataBean dataBean = scTravelList.get(position);
                    bundle.putSerializable("data", dataBean);
                    bundle.putStringArrayList("img", myTravelList.get(position).getImgList());
                    jump(TravelItemFragment.class, bundle, "travelItem");
                });
                //
                b.searchTravel.setSubmitButtonEnabled(true);
                b.searchTravel.setOnCloseListener(() -> {
                    travelViewModel.search(scTravelList, "");
                    travelRvAdapter.notifyDataSetChanged();
                    return false;
                });
                b.searchTravel.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                    @Override
                    public boolean onQueryTextSubmit(String query) {
                        b.searchTravel.setIconified(true);
                        travelViewModel.search(scTravelList, query);
                        travelRvAdapter.notifyDataSetChanged();
                        return true;
                    }

                    @Override
                    public boolean onQueryTextChange(String newText) {
                        return false;
                    }
                });

            } else {
                ToastUtils.showShort("加载失败");
            }
        });
    }

    @Override
    protected String setTitle() {
        return "智慧旅游";
    }

    @Override
    protected boolean showBack() {
        return true;
    }

    @Override
    protected Boolean showBottom() {
        return null;
    }
}
